#include <wchar.h>
#include "Syscall.h"
#include "Private.h"
#include "Database.h"
#include "System.h"
#include "User.h"

/// <summary>
/// Locals
/// </summary>
static Database users;

/// <summary>
/// The GetUserID function 
/// returns the real user ID 
/// of the current process.
/// </summary>
UID GetUserID(void)
{
	UID uid = -1;
	if ((uid = Syscall0(__NR_getuid)) < 0)
		return -1;
	return uid;
}

/// <summary>
/// The SetCurrentUserID function 
/// sets the effective user ID of the
/// current process. If the effective UID of
/// the caller is root, the real UID and
/// saved set-user-ID are also set.
/// </summary>
int SetCurrentUserID(UID uid)
{
	int result = -1;
	if ((result = Syscall1(__NR_setuid, uid)) < 0)
		return -1;
	return result;
}

/// <summary>
/// The GetUserID function 
/// returns the real user ID 
/// of the current process.
/// </summary>
int GetUserFromID(UID uid, UserPtr user)
{
	if (uid < 0)
		return -1;

	if (user == NULL)
		return -1;

	if (DatabaseOpen(DATABASE_USERS, &users) < 0)
		return -2;

	DatabaseClose(&users);

	return 0;
}

/// <summary>
/// The CreateUser function 
/// returns the real user ID 
/// of the current process.
/// </summary>
int CreateUser(UserPtr data)
{
	if (data == NULL)
		return -1;

	if (GetGroupID() > 0)
		return SYS_NOACCESS;

	return SYS_OK;
}

/// <summary>
/// The CreateUser function 
/// returns the real user ID 
/// of the current process.
/// </summary>
int DeleteUser(UID uid)
{
	if (uid < 1)
		return -1;

	if (GetGroupID() > 0)
		return SYS_NOACCESS;

	return SYS_OK;
}

